﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBUtility;



namespace DAL
{
    public class AuthorityDAL
    {

        //通过userid判断用户权限
        public string GetGroupIDByUserid(string id)
        {
            StringBuilder sql = new StringBuilder();
            DataSet ds = new DataSet();
            string group_id = string.Empty;
            sql.Append(" select  m.group_id from sys_master t,sys_mastergroup m where m.master_id=t.id and  t.id='" + id + "'");

            if (sql != null && sql.ToString() != "")
            {
                try
                {
                    DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                    ds = DbHelperSQL.Query(sql.ToString());

                    if (ds != null && ds.Tables[0].Rows.Count != 0)
                    {
                        group_id = ds.Tables[0].Rows[0]["group_id"].ToString();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return group_id;
        }

        //判断用户是否有该功能的权限
      public string GetPDAAuthority(string userid)
      {
         string result = string.Empty;
          StringBuilder sql = new StringBuilder();
          DataSet ds = new DataSet();
          string groupid = GetGroupIDByUserid(userid);
          if (groupid != null && groupid != "")
          {               
             sql.Append(" select * from sys_action t right join sys_actiongroup g");
             sql.Append("  on  t.action=g.action where t.action_columnid='21' and group_id='" + groupid + "'");//(select GROUP_ID from sys_mastergroup where MASTER_ID=
             try{
                     DbHelperSQL.connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                    ds = DbHelperSQL.Query(sql.ToString());

                    for(int i =0; i< ds.Tables[0].Rows.Count;i++)
                    {
                    result +=   ds.Tables[0].Rows[i]["action"].ToString()+',';
                    }
              }
             catch (Exception ex)
             {
                 throw ex;
             }
         }
          return result;
      }
    }
}
